package com.aaa.ddd.service;

import com.aaa.pojo.Dept;
import com.aaa.pojo.PageSort;
import com.aaa.pojo.Users;
import com.aaa.ddd.dao.UserDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@EnableScheduling
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    public UserServiceImpl() {
    }

    public Page findAllUser(PageSort pageSort) {
        PageHelper.startPage(pageSort.getPage(), pageSort.getLimit());
        List<Users> UserInfoList = this.userDao.findAllUser(pageSort);
        Page listWithPage = (Page)UserInfoList;
        return listWithPage;
    }

    public boolean findNoSameName(Users UserInfo) {
        List<com.aaa.pojo.Users> UserInfoList = this.userDao.findNoSameName(UserInfo);
        return UserInfoList == null || UserInfoList.size() <= 0;
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    public boolean addUser(Users users) {
        int num = 0;
        int i = this.userDao.addUser(users);
        if (i > 0) {
            List<com.aaa.pojo.Users> usersList = this.userDao.findUserIdByName(users.getUsername());
            Integer userid = usersList.get(0).getUserid();
            Integer roleId = users.getRoleId();
            num = this.userDao.addUserRole(userid, roleId);
        }

        return num > 0;
    }

    public boolean deleteUserById(String[] userids) {
        int num = this.userDao.deleteUserById(userids);
        return num > 0;
    }

    public boolean resetPassword(Users UserInfo) {
        int num = this.userDao.resetPassword(UserInfo);
        return num > 0;
    }

    public boolean updateUser(Users UserInfo) {
        int num = this.userDao.updateUser(UserInfo);
        return num > 0;
    }

    public List<Map<String, Object>> findAllUserInfoByName(Map map) {
        return this.userDao.findAllUserInfoByName(map);
    }

    public List<Dept> queryAllDept() {
        return this.userDao.queryAllDept();
    }

    public Users selectById(Integer userid) {
        List<Users> UserInfoList = this.userDao.selectById(userid);
        return (Users)UserInfoList.get(0);
    }

    public boolean updateOneself(Users UserInfo) {
        int num = this.userDao.updateOneself(UserInfo);
        return num > 0;
    }
}
